//
//  HotDetailsViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/1.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "HotDetailsViewController.h"
#import "BMLcleView.h"
#import "NetworkHandler.h"
#import "HotModel.h"
#import "UIImageView+WebCache.h"
#import "HotDetailsCollectionViewCell.h"
#import "HotRightCollectionViewCell.h"
#import "ToolModel.h"
#import "TAMacro.h"
#import "ToolDB.h"
@interface HotDetailsViewController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic, strong)BMLcleView *cysleView;
@property (nonatomic,strong)UIView * MiddleView;
@property (nonatomic,strong)UIScrollView * scrollView;
@property (nonatomic,strong)UILabel * descriptionLable;
@property (nonatomic,strong)UILabel * nameLable;
@property (nonatomic,strong)UILabel * priceLable;
@property (nonatomic,strong)NSMutableArray * array;
@property (nonatomic,strong)UIButton * Right_button;
@property (nonatomic,strong)UIButton * Left_button;
@property (nonatomic,strong)UICollectionView * collectionView;
@property (nonatomic,strong)HotDetailsCollectionViewCell * LeftCell;
@property (nonatomic,strong)HotRightCollectionViewCell * RightCell;
@property (nonatomic,strong)UIButton * like_Button;
@property (nonatomic,strong)UIButton * buy_Button;
@property (nonatomic,strong)UIAlertController * alertController;
@property (nonatomic,strong)UIAlertController * actionSheet;

@end

@implementation HotDetailsViewController
- (void)viewDidAppear:(BOOL)animated{
    NSString *sql = @"SELECT title,cover_image_url,Id FROM t_Hot;";
    NSMutableArray *array = [ToolDB selectFromSql:sql];
    
    BOOL saved = NO;
    for (ToolModel *model in array) {
        if ([model.title isEqualToString:self.name]) {
            saved = YES;
            [self.like_Button setImage:[UIImage imageNamed:@"红心"] forState:UIControlStateNormal];
            
            break;
            
        }
    }
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.translucent=NO;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [butt setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    butt.frame  = CGRectMake(0, 0, 50, 50);
    UIBarButtonItem *left= [[UIBarButtonItem alloc]initWithCustomView:butt];
    [butt addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =left;
    
    self.navigationItem.title = @"详情";
    
    [self FoundUIScrollView];
    [self foundTwoButton];
    
    [self PlaypictureView];
    [self Down];
    [self Playpicture];
    
}


- (void)FoundUIScrollView{
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH , SCREEN_HEIGHT)];
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT * 2 );
    self.scrollView.bounces = NO;
    self.scrollView.delegate =self;
    
    
}

- (void)foundTwoButton{

    self.like_Button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.like_Button.frame =CGRectMake(0, SCREEN_HEIGHT - SCREEN_HEIGHT / 10 - 64 , SCREEN_WIDTH , SCREEN_HEIGHT / 10);
    [self.like_Button setTitle:@"喜欢" forState:UIControlStateNormal];
    
    [self.like_Button jxl_setDayMode:^(UIView *view) {
        self.like_Button.backgroundColor = [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        self.like_Button.backgroundColor = [UIColor grayColor];
        
    }];
    
    
    
    [self.like_Button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.like_Button setImage:[UIImage imageNamed:@"心"] forState:UIControlStateNormal];
    [self.like_Button addTarget:self action:@selector(like:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.like_Button];
    
    
    
    
}


// 收藏

- (void)like:(UIButton*)send{
    
    
    
    NSString *sql = @"SELECT title,cover_image_url,Id FROM t_Hot;";
    NSMutableArray *array = [ToolDB selectFromSql:sql];
    
    BOOL saved = NO;
    for (ToolModel *model in array) {
        if ([model.title isEqualToString:self.name]) {
            saved = YES;
            break;
        }
    }
    
    if (saved == NO) {
        [ToolDB createTableTwo];
        
        [self.like_Button setImage:[UIImage imageNamed:@"红心"] forState:UIControlStateNormal];
        
        NSString *sql2 = [NSString stringWithFormat:@"INSERT INTO t_Hot(title,cover_image_url,ID) VALUES('%@','%@',%@);", self.name,self.IMAGE,self.ID];
        
        [ToolDB execSqlString:sql2];
        
        
    }else{
        
        [self.like_Button setImage:[UIImage imageNamed:@"心"] forState:UIControlStateNormal];
        
        
        NSString *sql2 = [NSString stringWithFormat:@"DELETE FROM t_Hot where title = '%@' and cover_image_url = '%@';", self.name,self.IMAGE];
        
        [ToolDB dele:sql2];
        
    }
    
    
    
    
    
    
}



// 轮播图View
- (void)PlaypictureView{
    
    NSMutableArray *array = [NSMutableArray array];
    self.cysleView = [[BMLcleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT / 3 * 2) DataSource:array TimeInterval:0];
    [self.scrollView addSubview:_cysleView];
    
    
}


// 轮播图
- (void)Playpicture{
    
    
    // 属性传值第二步
    NSInteger URL = self.str;
    
    [NetworkHandler getDataByURLString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/items/%ld",(long)URL] BodyString:nil WithDataBlock:^(id result) {
        
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"image_urls"];
        [_cysleView reloadDataByArray:arr];
        
        
        self.MiddleView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 3 * 2, SCREEN_WIDTH, SCREEN_HEIGHT / 3   )];
        [self.scrollView addSubview:self.MiddleView];
        
        
        self.nameLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT /  3 / 5 * 2)];
        [self.MiddleView addSubview:self.nameLable];
        self.nameLable.font = [UIFont systemFontOfSize:25];
        self.nameLable.text = [dic objectForKey:@"name"];
        
        
        self.priceLable = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 8 , SCREEN_WIDTH, SCREEN_HEIGHT /  3 / 5 )];
        [self.MiddleView addSubview:self.priceLable];
        self.priceLable.text = [NSString stringWithFormat:@"$%@",[dic objectForKey:@"price"]];
        self.priceLable.textColor = [UIColor redColor];
        self.priceLable.backgroundColor = [UIColor whiteColor];
        self.priceLable.font = [UIFont systemFontOfSize:23];
        self.comments_count = [NSString stringWithFormat:@"%@", [dic objectForKey:@"comments_count"]];
        
        
        
        [self DownTwoButton];
        
        
        
        self.descriptionLable = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 8 + SCREEN_HEIGHT / 3 / 5, SCREEN_WIDTH, SCREEN_HEIGHT /  3 / 5 *2 + 5)];
        self.descriptionLable.backgroundColor = [UIColor whiteColor];
        [self.MiddleView addSubview:self.descriptionLable];
        self.descriptionLable.numberOfLines = 0;
        self.descriptionLable.textAlignment = 3;
        self.descriptionLable.text =[dic objectForKey:@"description"];
        
        self.detail_html = [dic objectForKey:@"detail_html"];
        
        self.ID = [dic objectForKey:@"id"];
        
        
        UIView *black =  [[UIView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT / 8, SCREEN_WIDTH, 2)];
        black.backgroundColor = [UIColor grayColor];
        [self.MiddleView addSubview:black];
        
        
        [self.LeftCell.webView  loadHTMLString:self.detail_html baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];
        
        [self.collectionView reloadData];
        
        
        
        
    }];
    
    
    
    
}

- (void)DownTwoButton{
    
    self.Left_button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.Left_button.frame =CGRectMake(0, SCREEN_HEIGHT  , SCREEN_WIDTH / 2, SCREEN_HEIGHT / 10);
    self.Left_button.backgroundColor = [UIColor whiteColor];
    [self.Left_button setTitle:@"图文介绍" forState:UIControlStateNormal];
    [self.Left_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.scrollView addSubview:self.Left_button];
    [self.Left_button addTarget:self action:@selector(ToRight:) forControlEvents:UIControlEventTouchUpInside];
    
    self.Right_button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.Right_button.frame =CGRectMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT , SCREEN_WIDTH / 2, SCREEN_HEIGHT / 10);
    self.Right_button.backgroundColor = [UIColor whiteColor];
    [self.Right_button setTitle:@"评论"forState:UIControlStateNormal];
    [self.Right_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.scrollView addSubview:self.Right_button];
    [self.Right_button addTarget:self action:@selector(ToLeft:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *black =[[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT , 2, SCREEN_HEIGHT  / 10)];
    black.backgroundColor = [UIColor grayColor];
    [self.scrollView addSubview:black];
    
    UIView *black2 =[[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH,2)];
    black2.backgroundColor = [UIColor grayColor];
    [self.scrollView addSubview:black2];
    
    
    
}


- (void)Down{
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); // 内边距
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT + SCREEN_HEIGHT / 10 , SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout
                           ];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    [self.scrollView addSubview:_collectionView];
    
    // 注册cell
    
    [self.collectionView registerClass:[HotDetailsCollectionViewCell class] forCellWithReuseIdentifier:@"LeftCell"];
    
    [self.collectionView registerClass:[HotRightCollectionViewCell class] forCellWithReuseIdentifier:@"RightCell"];
    
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.item == 0) {
        self.LeftCell= [collectionView dequeueReusableCellWithReuseIdentifier:@"LeftCell" forIndexPath:indexPath];
        
        
        return self.LeftCell;
        
    }else{
        
        self.RightCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RightCell" forIndexPath:indexPath];
        
        self.RightCell.Str = self.ID;
        
        return self.RightCell;
        
    }
    
    
    
    
}


- (void)ToLeft:(UIButton *)sender{
    
    NSInteger i = self.collectionView.contentOffset.x / SCREEN_WIDTH;
    if (i == 0) {
        [self.collectionView setContentOffset:CGPointMake(SCREEN_WIDTH , 0) animated:YES];
    }
    
    
}
- (void)ToRight:(UIButton *)sender{
    NSInteger i =   self.collectionView.contentOffset.x / SCREEN_WIDTH;
    
    if (i == 1) {
        [self.collectionView setContentOffset:CGPointMake(0 , 0) animated:YES];
    }
    
    
    
}

- (void)clickLeft:(UIBarButtonItem*)qwe{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}

- (void)viewWillDisappear:(BOOL)animated{
    
    self.navigationController.navigationBar.translucent=NO;
    
    
}
@end
